<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends WD_Controller {

    public function index(){

        $search_root = $this->config->item('search_root');

        $treeData = \libraries\waydiao\SearchData::getTreeData();

        $data = [
            'host' => $search_root,
            'treeData' => $treeData,
        ];

        $this->load->view('search/index', $data);
    }

	/**
	 * API
	 */
	public function detail(){

		$uri = isset($_GET["uri"]) ? addslashes($_GET["uri"]) : "";

        $search_root = $this->config->item('search_root');

		$api = \libraries\waydiao\SearchData::getApiByUri($uri);

		$data = [
			'host' => $search_root,
			'api' => $api,
		];

		$this->load->view('search/detail', $data);

	}

	public function asyncDetail(){

		$uri = isset($_GET["uri"]) ? addslashes($_GET["uri"]) : "";
		$api = \libraries\waydiao\SearchData::getApiByUri($uri);

		$data = [];
		if(isset($api["params"]) && is_array($api["params"])) {
			foreach ($api["params"] as $param) {
				$data[$param["name"]] = isset($_POST[$param["name"]]) ? $_POST[$param["name"]] : $param["default"];
			}
		}

		echo "请求参数：";
		pre($data);

		//给未urlencod的中文参数urlencode
		foreach($data as & $line){
            $line = urlencode(urldecode($line));
        }

        $host = isset($this->config->item('search')["host"]) ? $this->config->item('search')["host"] : "";

		$request_url = $host.$api["uri"];

		echo "请求地址：".$request_url."<br>";

		if(strtoupper($api["method"]) == 'POST'){

		}else{
			$r = curl_get_request($request_url, $data);
		}
		var_dump($r);

		$r2 = json_decode($r, true);
		pre($r2);

	}

}
